package com.arcvideo.acsplayer.dialog;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.arcvideo.acsplayer.R;


public class NormalDialog extends Dialog implements
		View.OnClickListener {
	public static final int DIALOG_NONE = 0;
	public static final int DIALOG_WIFI_ERROR = 1;
	public static final int DIALOG_EXIT_APP = 2;
	public static final int DIALOG_VR = 3;
	
	private Context mContext = null;
	private TextView mTitleView = null;
	private TextView mMessageView = null;
	private Button mBtnConfirm = null;
	private Button mBtnCancel = null;
	private Button mBtnRemand = null;
	private ClickListener mClickListener = null;
	private int mTitleId = -1;
	private int mMessageId = -1;
	private int mLeftButtonId = -1;
	private int mRightButtonId = -1;
	private String mMessage = null;
	private boolean mIsRemand = false;

	public interface ClickListener {
		public void confirm();
		public void cancel();
		public void remand();
	}

	public NormalDialog(Context context) {
		super(context);
		mContext = context;
	}

	public NormalDialog(Context context, int theme, String messge,
			ClickListener clickListener) {
		super(context, theme);
		mContext = context;
		mMessage = messge;
		mClickListener = clickListener;
	}

	public NormalDialog(Context context, int theme, int messgeId,
			ClickListener clickListener) {
		super(context, theme);
		mContext = context;
		mMessageId = messgeId;
		mClickListener = clickListener;
	}

	public NormalDialog(Context context, int theme, int titleId, int messgeId,
			boolean isRemand, int leftId, int rightId, ClickListener clickListener) {
		super(context, theme);
		mContext = context;
		mTitleId = titleId;
		mMessageId = messgeId;
		mIsRemand = isRemand;
		mLeftButtonId = leftId;
		mRightButtonId = rightId;
		mClickListener = clickListener;
	}

	public NormalDialog(Context context, int theme, int messgeId,
			boolean isRemand, ClickListener clickListener) {
		super(context, theme);
		mContext = context;
		mMessageId = messgeId;
		mIsRemand = isRemand;
		mClickListener = clickListener;
	}

	public NormalDialog(Context context, int theme, int titleId, int messgeId,
			ClickListener clickListener) {
		super(context, theme);
		mContext = context;
		mTitleId = titleId;
		mMessageId = messgeId;
		mClickListener = clickListener;
	}

	public NormalDialog(Context context, int theme, int titleId, int messgeId,
			boolean isRemand, ClickListener clickListener) {
		super(context, theme);
		mContext = context;
		mTitleId = titleId;
		mMessageId = messgeId;
		mIsRemand = isRemand;
		mClickListener = clickListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		init();
	}

	public void init() {
		LayoutInflater inflater = LayoutInflater.from(mContext);
		View view = inflater.inflate(R.layout.dialog_normal, null);
		setContentView(view);
		setCancelable(false);
		
		// Title and Text
		mTitleView = (TextView) view.findViewById(R.id.remand_title);
		mMessageView = (TextView) view.findViewById(R.id.remand_text);
		if (mTitleId != 0) {
			mTitleView.setText(mTitleId);
		}
		if (mMessageId != -1) {
			mMessageView.setText(mMessageId);
		} else if (mMessage != null) {
			mMessageView.setText(mMessage);
		}

		// OK and Cancel
		mBtnConfirm = (Button) view.findViewById(R.id.confirm);
		if (mRightButtonId != -1 && null != mBtnConfirm) {
			mBtnConfirm.setText(mRightButtonId);
		}
		mBtnCancel = (Button) view.findViewById(R.id.cancel);
		if (mLeftButtonId != -1 && null != mBtnCancel) {
			mBtnCancel.setText(mLeftButtonId);
		}
		mBtnRemand = (Button) view.findViewById(R.id.ok);

		if (mIsRemand) {
			if (null != mBtnConfirm)
				mBtnConfirm.setVisibility(View.GONE);
			if (null != mBtnCancel)
				mBtnCancel.setVisibility(View.GONE);
			if (null != mBtnRemand) {
				mBtnRemand.setVisibility(View.VISIBLE);
				mBtnRemand.setOnClickListener(this);
			}
		} else {
			if (null != mBtnRemand)
				mBtnRemand.setVisibility(View.GONE);
			if (null != mBtnConfirm) {
				mBtnConfirm.setOnClickListener(this);
				mBtnConfirm.setVisibility(View.VISIBLE);
			}
			if (null != mBtnCancel) {
				mBtnCancel.setOnClickListener(this);
				mBtnCancel.setVisibility(View.VISIBLE);
			}
		}

	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			dismiss();
			mClickListener.confirm();
			break;
		case R.id.cancel:
			dismiss();
			mClickListener.cancel();
			break;
		case R.id.ok:
			dismiss();
			mClickListener.remand(); 
			break;
		}
	}
}
